package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.MessageBiz;
import com.egao.orderB.entity.Message;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-07-14 10:39:02
 * @version ：1.0
 */
@Repository
public class MessageBizImpl extends BaseDao implements MessageBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Message message, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Message> rm = new BeanPropertyRowMapper<Message>(Message.class);
		StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.content");
		sql.append(",m.sender,m.send_time,m.bill_of_lading,m.customer_receiver,m.receiver");
		sql.append(",m.type,m.state,m.order_num,m.create_time");
		sql.append(",m.create_user,m.update_time,m.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_message m ");
		sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
		sql.append(" where m.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
				sql.append(" and m.organization_id=:tableOrganizationId ");
				map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
			}
		if(Utils.isNotEmpty(message)){
			if(Utils.isNotEmpty(message.getType())){
				sql.append(" and instr(m.type,:type)>0 ");
				map.put("type", message.getType()+"");
			}
			if(Utils.isNotEmpty(message.getCustomerReceiver())){
				sql.append(" and instr(m.customer_receiver,:customer_receiver)>0 ");
				map.put("customer_receiver", message.getCustomerReceiver()+"");
			}
			if(Utils.isNotEmpty(message.getSort())&&Utils.isNotEmpty(message.getOrder())){
				sql.append(" order by m."+message.getSort()+" "+message.getOrder());
			} else {
				sql.append(" order by m.create_time desc");
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Message> list(Message message, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Message> rm = new BeanPropertyRowMapper<Message>(Message.class);
			StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.content");
			sql.append(",m.sender,m.send_time,m.bill_of_lading,m.customer_receiver,m.receiver");
			sql.append(",m.type,m.state,m.order_num,m.create_time");
			sql.append(",m.create_user,m.update_time,m.update_user");
			sql.append(",sogzn.organization_name organizationName,suser.username createName ");
			sql.append(" from chf_message m ");
			sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
			sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
			sql.append(" where m.deleted=0 ");
			if(loginUser.getUserId() != UserConstant.ADMIN_ID){
				sql.append(" and m.organization_id=:tableOrganizationId ");
				map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
			}
    	if(Utils.isNotEmpty(message)){
				if(Utils.isNotEmpty(message.getType())){
					sql.append(" and instr(m.type,:type)>0 ");
					map.put("type", message.getType()+"");
				}
				if(Utils.isNotEmpty(message.getCustomerReceiver())){
					sql.append(" and instr(m.customer_receiver,:customer_receiver)>0 ");
					map.put("customer_receiver", message.getCustomerReceiver()+"");
				}
				if(Utils.isNotEmpty(message.getSort())&&Utils.isNotEmpty(message.getOrder())){
				sql.append(" order by m."+message.getSort()+" "+message.getOrder());
			} else {
				sql.append(" order by m.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Message getById(Message message) {
		RowMapper<Message> rm = new BeanPropertyRowMapper<Message>(Message.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.content");
        sql.append(",m.sender,m.send_time,m.bill_of_lading,m.customer_receiver,m.receiver");
		sql.append(",m.type,m.state,m.order_num,m.create_time");
		sql.append(",m.create_user,m.update_time,m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_message m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 and m.id=:id ");
		map.put("id", message.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Message message, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_message (");
		sql.append("organization_id,content,bill_of_lading,sender,send_time");
		sql.append(",customer_receiver,receiver,type,state");
		sql.append(",order_num,create_user,create_time) values ( ");
		sql.append(":organization_id,:content,:bill_of_lading,:sender,now()");
		sql.append(",:customer_receiver,:receiver,:type,:state");
		sql.append(",:order_num,:create_user,now())");
		map.put("organization_id", loginUser.getOrganizationId() + "");
		map.put("content", message.getContent());
		map.put("bill_of_lading", message.getBillOfLading());
		map.put("sender", Utils.isNotEmpty(message.getSender())?message.getSender():loginUser.getUserId()+"");
		map.put("customer_receiver", message.getCustomerReceiver()==null?null:message.getCustomerReceiver()+"");
		map.put("receiver", message.getReceiver());
		map.put("type", message.getType());
		map.put("state", message.getState());
		map.put("order_num", message.getOrderNum()==null?null:message.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Message message, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_message set ");
        sql.append("content=:content,sender=:sender,send_time=:send_time ");
		sql.append(",bill_of_lading=:bill_of_lading,customer_receiver=:customer_receiver,receiver=:receiver,type=:type ");
		sql.append(",state=:state,order_num=:order_num,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("content", message.getContent());
        map.put("sender", message.getSender());
        map.put("send_time", message.getSendTime());
        map.put("bill_of_lading", message.getBillOfLading());
        map.put("customer_receiver", message.getCustomerReceiver()==null?null:message.getCustomerReceiver()+"");
        map.put("receiver", message.getReceiver());
        map.put("type", message.getType());
        map.put("state", message.getState());
        map.put("order_num", message.getOrderNum()==null?null:message.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", message.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Message message, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(message.getId().length()>0){
			for(String ID : message.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_message set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
